package wetalk.software.bupt.com.wetalk.adapter;

import android.content.ComponentName;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.IBinder;
import android.support.v7.app.AlertDialog;
import android.text.Layout;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.jyt.message.Message;
import com.jyt.message.MessageConfig;
import com.jyt.util.MySerializable;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.LinkedHashMap;
import java.util.List;

import wetalk.software.bupt.com.wetalk.DB.WeTalkDB;
import wetalk.software.bupt.com.wetalk.GloableParams;
import wetalk.software.bupt.com.wetalk.R;
import wetalk.software.bupt.com.wetalk.application.UserManager;
import wetalk.software.bupt.com.wetalk.communicate.AndroidClient;
import wetalk.software.bupt.com.wetalk.communicate.AndroidClientService;
import wetalk.software.bupt.com.wetalk.model.po.Applicant;
import wetalk.software.bupt.com.wetalk.model.po.User;
import wetalk.software.bupt.com.wetalk.view.activity.NewFriendsActivity;
import wetalk.software.bupt.com.wetalk.view.activity.VerifyActivity;
import wetalk.software.bupt.com.wetalk.view.dialog.CustomDialog;

/**
 * 好友申请列表的适配器
 */

public class NewFriendsAdapter extends BaseAdapter {
    private static final String TAG="NewFriendsAdapter";

    private Context mContext;
    private List<Applicant> mList;
    private OnAcceptListener onAcceptListener;


    public NewFriendsAdapter(Context context,List<Applicant> list){
        mContext=context;
        mList=list;

    }
    @Override
    public int getCount() {
        return mList.size();
    }

    @Override
    public Object getItem(int position) {
        return mList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        final Applicant apt=mList.get(position);
        ViewHolder viewHolder=new ViewHolder();
        if(convertView==null){
            convertView=LayoutInflater.from(mContext).inflate(R.layout.new_friend_item,parent,false);
            viewHolder.img= (ImageView) convertView.findViewById(R.id.new_friend_img);
            viewHolder.tvName=(TextView)convertView.findViewById(R.id.new_friend_name);
            viewHolder.tvMsg=(TextView)convertView.findViewById(R.id.new_friend_msg);
            viewHolder.tvAddOk=(TextView)convertView.findViewById(R.id.new_friend_add_ok);//已添加
            viewHolder.btAccept=(Button)convertView.findViewById(R.id.new_friend_accept);//同意按钮
            viewHolder.layout=(LinearLayout)convertView.findViewById(R.id.new_friend_layout) ;
            convertView.setTag(viewHolder);
        } else{
            viewHolder= (ViewHolder) convertView.getTag();
        }
        viewHolder.img.setImageResource(R.drawable.a5);
        viewHolder.tvName.setText(apt.getName());
        viewHolder.tvMsg.setText(apt.getMessage());

        viewHolder.layout.setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View v) {
                final Applicant apt=mList.get(position);
                AlertDialog.Builder dialog=new AlertDialog.Builder(mContext);
                dialog.setTitle("删除申请人");
                dialog.setMessage("确定删除该申请人？");
                dialog.setCancelable(false);
                dialog.setPositiveButton("是", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        WeTalkDB.create(mContext).deleteApplicant(apt.getId());
                        mList.remove(position);
                        notifyDataSetChanged();
                    }
                });
                dialog.setNegativeButton("否", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                    }
                });
                dialog.show();
                Log.d(TAG,"删除申请人");
                return true;
            }
        });


        //点击同意按钮触发事件
        viewHolder.btAccept.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View v) {
                onAcceptListener.onAccept(position);
            }
        });

        if(apt.getStatus()==1){
            viewHolder.tvAddOk.setVisibility(View.VISIBLE);
            viewHolder.btAccept.setVisibility(View.GONE);
        }

        return convertView;
    }

    class ViewHolder {
        ImageView img;
        TextView tvName,tvMsg,tvAddOk;
        Button btAccept;
        LinearLayout layout;
    }


    public interface OnAcceptListener{

        void onAccept(int position);
    }

    public void setOnAcceptListener(OnAcceptListener listener){
        onAcceptListener=listener;
    }
}
